#!/usr/bin/env python

import os
import sys
from optparse import HelpFormatter

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT)

import upload


class GCWikiHelpFormatter (HelpFormatter):
    """Format help with wiki markup for Google Code."""

    def __init__(self,
                 indent_increment=2,
                 max_help_position=24,
                 width=None,
                 short_first=1):
        HelpFormatter.__init__(
            self, indent_increment, max_help_position, width, short_first)
        self._dl_open = False

    def indent(self):
        self._pending = 'INDENT'
        HelpFormatter.indent(self)

    def dedent(self):
        self._pending = 'DEDENT'
        HelpFormatter.dedent(self)

    def format_usage(self, usage):
        return "*Usage summary:* `%s`\n" % usage

    def format_heading(self, heading):
        if self._dl_open:
            pre = '\n</dl>\n'
        else:
            pre = ''
        markup = '='*(self.current_indent+2)
        self._dl_open = True
        return "%s%s %s %s\n<dl>\n" % (pre, markup, heading, markup)

    def format_option(self, option):
        result = []
        opts = self.option_strings[option]
        result.append('<dt>`%s`</dt>\n' % opts)
        if option.help:
            help_text = '<dd>%s</dd>\n' % self.expand_default(option)
            result.append(help_text)
        return ''.join(result)


def main():
    upload.parser.formatter = GCWikiHelpFormatter()
    print HEADER
    print upload.parser.format_option_help()
    print '</dl>'  # TODO: Formatter should do this
    print FOOTER
    print

HEADER = """#summary upload.py usage and options.
<wiki:comment>
THIS PAGE IS AUTOGENERATED. DO NOT EDIT.
To update this page run tools/uploadopts2wiki.py
</wiki:comment>

= upload.py Usage =

[http://codereview.appspot.com/static/upload.py upload.py] is a tool
for uploading diffs from a version control system to the codereview app.

*Usage summary:*

{{{upload.py [options] [-- diff_options]}}}

Diff options are passed to the diff command of the underlying system.

*Supported version control systems:*
 * Git
 * Mercurial
 * Subversion
 * Perforce

It is important for Git/Mercurial users to specify a tree/node/branch to diff
against by using the '--rev' option.

"""

FOOTER = """\

==== Running upload.py from perforce GUIs ====

You can right click on a perforce changelist and create a new Rietveld code review by adding a custom tool with the following settings:

Application: python Arguments: /PATH/TO/upload.py -s MY_SERVER --p4_changelist %p --p4_port $p --p4_user $u --p4_client $c Start In: empty. Check "Add to applicable context menus", "Run tool in terminal window" (or system equivalent), and "Ignore P4CONFIG files".

Replace /PATH/TO/ with the location of upload.py, and MY_SERVER with the rietveld code review server. See screenshot [http://alexmccarthy.net/Rietveld%20-%20P4V%20Custom%20Tool%20Settings.png here].
"""

if __name__ == '__main__':
    main()
